package components.valueobjects
{
	import flash.globalization.DateTimeFormatter;
	import flash.globalization.LocaleID;
	
	import utils.nfc.NfcInterface;
	import utils.nfc.NfcTagCall;
	import utils.nfc.NfcTagEvent;
	import utils.nfc.NfcTagMapUrl;
	import utils.nfc.NfcTagUrl;

	[Bindable]
	public class EventVO
	{
		public static const TYPE_EVENT:String="event";
		private static const imgPath:String="images/event/";


		public var title:String;
		public var img:String;
		public var description:String;
		public var location:String;
		public var url:String;
		public var phone:String;
		public var price:String;
		public var hours:String;
		public var mapurl:String;
		public var address:AddressVO;
		public var date:DateVO;
		public var orgStartDate:String;
		public var orgEndDate:String;
		
		public var dateString:String;
		public var remainingString:String;
		public var remainingDays:Number;


		public function EventVO(title:String, img:String, description:String, location:String, url:String, phone:String, price:String, hours:String, address:AddressVO, date:DateVO,orgStartDate:String,orgEndDate:String, mapurl:String)
		{
			this.title=title;
			this.img=imgPath + img;
			this.description=description;
			this.location=location;
			this.url=url;
			this.phone=phone;
			this.price=price;
			this.hours=hours;
			this.mapurl=mapurl;
			this.address=address;
			this.date=date;
			
			// Diese beiden Orginal XML Strings werden für die Erzeugung
			// des NFC Inhalts verwendet
			this.orgStartDate = orgStartDate;
			this.orgEndDate = orgEndDate;
			
			// String: Um die verbleibenden Zeit anzuzeigen
			// Number für die Sortierung der Liste
			this.remainingDays=getRemainingDays(date.start);
			this.remainingString=getRemainingString(remainingDays);
			// So wird der String in der oberfläche angezeigt
			this.dateString = getDateString(this.date.start,this.date.end);
		}
		
		
		public function getNfcContent():Array{
			var nfcContent:Array = new Array();
			nfcContent.push(new NfcTagUrl(NfcInterface.POSITION_TOP,url));
			nfcContent.push(new NfcTagEvent(NfcInterface.POSITION_RIGHT,this));
			nfcContent.push(new NfcTagCall(NfcInterface.POSITION_BOTTOM,this.phone));
			nfcContent.push(new NfcTagMapUrl(NfcInterface.POSITION_LEFT,this.mapurl,this.location));
			
			return nfcContent;
		}
		
		private function getDateString(start:Date,end:Date):String{
			return getDateTimeString(start)+"\n-\n"+getDateTimeString(end);
		}
		
		// Gibt den String aus, der nacher angezeigt wird, abhängig von dem Boolean Allday		
		private function getDateTimeString(date:Date):String{
			var dateformatter:DateTimeFormatter = new DateTimeFormatter(LocaleID.DEFAULT);
			if(this.date.allday){
				dateformatter.setDateTimePattern("dd.MM.yy");
				return dateformatter.format(date);
			}else{
				dateformatter.setDateTimePattern("dd.MM.yy - HH:mm");
				return dateformatter.format(date)+" Uhr";
			}
		}

		private function getRemainingString(remainingDays:Number):String
		{
			if (remainingDays > 0)
			{
				return "in " + (remainingDays+1) +" Tagen"
			}
			else if (remainingDays == 0)
			{
				return "Heute";
			}
			else
			{
				// Wenn StartDatum in der Vergangenheit, muss geprüft werden,
				// ob das Enddatum auch vorbei ist oder das Event noch läuft
				if (getRemainingDays(date.end) < 0)
				{
					return "vorbei";
				}
				else
				{
					return "aktuell";
				}
			}
		}

		private function getRemainingDays(targetDate:Date):Number
		{
			var today:Date=new Date();
			// Anzahl der Millisekunden zwischen heute und dem Datum
			var milliseconds:Number=targetDate.time - today.time;
			// Kommt noch oder heute
			if (milliseconds >= 0)
			{
				var seconds:Number=milliseconds / 1000;
				var minutes:Number=seconds / 60;
				var hours:Number=minutes / 60;
				var days:Number=Math.floor(hours / 24);

				return days;
			}
			else
			{
				return -1;
			}

		}
	}
}
